/**
 * 
 */
package com.gosophia.conference.action.starting;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.conferenceManagement.common.dto.ConferenceJobHandleDTO;
import com.gosophia.conferenceManagement.common.ws.ConferenceQueryWebService;
import com.gosophia.conferenceManagement.starting.dto.ConferenceApplicationDTO;
import com.gosophia.conferenceManagement.starting.dto.ConferenceStartingDTO;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.reportData.bean.UserInfoBean;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.ws.UserManagementWebService;

/**
 * 功能：进入会议申请审批界面
 * 
 * @author tianchunyu
 * @since 2011-3-22 Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Controller("ConferenceApprovalEnterAction")
@Scope("prototype")
public class ConferenceApprovalEnterAction extends BaseSupportAction {
	private static final long serialVersionUID = 1L;

	@Autowired
	private ConferenceQueryWebService conferenceQueryWebService;

	private Long jobId;

	private ConferenceStartingDTO conferenceStartingDTO;

	private ConferenceApplicationDTO conferenceApplicationDTO;

	private Long jobSourceId;//任务的来源人的UserInfo.userInfoId,用于设定页面上的默认的下一提交人
	@Autowired
	private TaskManagementWebService taskManagementWebService;
	@Autowired
	private UserManagementWebService userManagementWebService;
	@Autowired
	private JudgeCurrentUndertaker judgeCurrentUndertaker;
	// private List<JobDTO> JobDTOs;
	private List<ConferenceJobHandleDTO> handleRecords;

	private String handleMenu;

	private String userInfoDtosJson;
	// private String approvalJson;
	private Long creatorId;
	private final Logger logger = Logger.getLogger(this.getClass());

	@Override
	public String _execute() throws Exception {
		// 得到当前用户
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		creatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		//防止非法用户进行任务处理
		judgeCurrentUndertaker.judgeOperationAuthority(jobId);
		conferenceStartingDTO = conferenceQueryWebService
				.findConferenceMasterByJobId(jobId);
		jobSourceId = taskManagementWebService.findJobDetailByJobId(jobId).getSourceDTO().getUserInfoId();
		logger.debug(" approval enter 输出missionId:"
				+ conferenceStartingDTO.getMissionId());
		// 审核历史
		handleRecords = conferenceQueryWebService.findHandleRecords(
				conferenceStartingDTO.getMissionId(), "审批会议申请");
		JobDTO nowJob = taskManagementWebService.findJobDetailByJobId(jobId);
		userInfoDtosJson = generateUserJson();
		// approvalJson = generatepprovalJson();
		if (nowJob.getJobType().endsWith("取回会议申请")) {
			handleMenu = "取回";
		} else if (nowJob.getJobType().endsWith("发起会议")) {
			handleMenu = "定稿";
		} else if (nowJob.getJobType().endsWith("修改会议申请")) {
			handleMenu = "修改";
		}

		else {
			handleMenu = "审批";
		}
		return SUCCESS;
	}

	private String generateUserJson() {
		List<UserInfoDTO> userInfoDtos = userManagementWebService
				.findAllUserInfoHasAccount();
		if (userInfoDtos == null) {
			return null;
		}
		List<UserInfoBean> userInfoBeans = new ArrayList<UserInfoBean>();
		for (UserInfoDTO userInfoDto : userInfoDtos) {
			UserInfoBean userInfoBean = new UserInfoBean();
			userInfoBean.setId(userInfoDto.getUserInfoId());
			userInfoBean.setName(userInfoDto.getUserName());
			userInfoBean.setType("acc");
			userInfoBeans.add(userInfoBean);
		}

		userInfoDtosJson = JSONArray.fromObject(userInfoBeans).toString()
				.replace("\"", "\\\"");

		return userInfoDtosJson;
	}

	/**
	 * 生成下一处理人列表
	 * 
	 * @return
	 */
	// private String generatepprovalJson() {
	// List<UserInfoDTO> userInfoDtos = userManagementWebService
	// .findAllUserInfoHasAccount();
	// List<UserInfoBean> userInfoBeans = new ArrayList<UserInfoBean>();
	// for (UserInfoDTO userInfoDto : userInfoDtos) {
	// if (creatorId.equals(userInfoDto.getUserInfoId())) {
	// continue;
	// }
	// UserInfoBean userInfoBean = new UserInfoBean();
	// userInfoBean.setId(userInfoDto.getUserInfoId());
	// userInfoBean.setName(userInfoDto.getUserName());
	// userInfoBean.setType("acc");
	// userInfoBeans.add(userInfoBean);
	// }
	//
	// approvalJson = JSONArray.fromObject(userInfoBeans).toString().replace(
	// "\"", "\\\"");
	//
	// return approvalJson;
	// }

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public ConferenceStartingDTO getConferenceStartingDTO() {
		return conferenceStartingDTO;
	}

	public void setConferenceStartingDTO(
			ConferenceStartingDTO conferenceStartingDTO) {
		this.conferenceStartingDTO = conferenceStartingDTO;
	}

	// public List<JobDTO> getJobDTOs() {
	// return JobDTOs;
	// }
	//
	// public void setJobDTOs(List<JobDTO> jobDTOs) {
	// JobDTOs = jobDTOs;
	// }

	public String getHandleMenu() {
		return handleMenu;
	}

	public void setHandleMenu(String handleMenu) {
		this.handleMenu = handleMenu;
	}

	public String getUserInfoDtosJson() {
		return userInfoDtosJson;
	}

	public void setUserInfoDtosJson(String userInfoDtosJson) {
		this.userInfoDtosJson = userInfoDtosJson;
	}

	// public String getApprovalJson() {
	// return approvalJson;
	// }
	//
	// public void setApprovalJson(String approvalJson) {
	// this.approvalJson = approvalJson;
	// }

	public ConferenceApplicationDTO getConferenceApplicationDTO() {
		return conferenceApplicationDTO;
	}

	public void setConferenceApplicationDTO(
			ConferenceApplicationDTO conferenceApplicationDTO) {
		this.conferenceApplicationDTO = conferenceApplicationDTO;
	}

	public List<ConferenceJobHandleDTO> getHandleRecords() {
		return handleRecords;
	}

	public void setHandleRecords(List<ConferenceJobHandleDTO> handleRecords) {
		this.handleRecords = handleRecords;
	}

	public Long getCreatorId() {
		return creatorId;
	}

	public void setCreatorId(Long creatorId) {
		this.creatorId = creatorId;
	}

    public Long getJobSourceId() {
        return jobSourceId;
    }

    public void setJobSourceId(Long jobSourceId) {
        this.jobSourceId = jobSourceId;
    }
	
}